#include "CEUtilities.h"

#include "CEMouse.h"
#include "CEMath.h"
#include "CEGUILabel.h"
#include "CEGUIPictureBox.h"
#include "CEGUIScrollBar.h"


CEGUIScrollBar::CEGUIScrollBar(CEGUIElementBase * parent, int myid, string myname, 
		int offsetX, int offsetY, int width, int height, bool enable, bool visible)
		: CEGUIElementBase(parent, myid, myname, offsetX, offsetY, width, height, enable, visible)
{
	m_pLabelValue = NULL;
	m_fValue = 0;

	_bGUIEditPosControled = false;
	_bGUIEditSizeHControled = false;
	_bGUIEditSizeVControled = false;
	_bInControl = false;
}

CEGUIScrollBar::~CEGUIScrollBar()
{
	FinalCleanUp();	
}


void CEGUIScrollBar::FinalCleanUp()
{
	SAFE_DELETE(m_pPicBarBegin);
	SAFE_DELETE(m_pPicBarMiddle);
	SAFE_DELETE(m_pPicBarEnd);
	SAFE_DELETE(m_pPicTarget);

	SAFE_DELETE(m_pLabelValue);
}




void CEGUIScrollBar::SetPosition(int offsetXFromParent, int offsetYFromParent)
{
	CEGUIElementBase::SetPosition(offsetXFromParent, offsetYFromParent);
	m_pPicBarMiddle->Move(0,0);
	m_pPicBarBegin->Move(0,0);
	m_pPicBarEnd->Move(0,0);
	m_pPicTarget->Move(0,0);
	if (m_pLabelValue)
		m_pLabelValue->Move(0,0);
}



void CEGUIScrollBar::Move(int offsetx, int offsety)
{
	CEGUIElementBase::Move(offsetx,offsety);
	m_pPicBarMiddle->Move(0,0);
	m_pPicBarBegin->Move(0,0);
	m_pPicBarEnd->Move(0,0);
	m_pPicTarget->Move(0,0);
	if (m_pLabelValue)
		m_pLabelValue->Move(0,0);
}


HRESULT CEGUIScrollBar::Render()
{
	if(m_bVisible)
	{
		HRESULT r = S_OK;
		r |= m_pPicBarBegin->Render();
		r |= m_pPicBarEnd->Render();
		r |= m_pPicBarMiddle->Render();
		r |= m_pPicTarget->Render();

		if (m_pLabelValue)
			r |= m_pLabelValue->Render();

		return r;
	}
	else
		return S_FALSE;
}




float CEGUIScrollBar::GetValue()
{
	return m_fValue;
}


HRESULT CEGUIScrollBar::Update(float deltaTime)
{
	return S_OK;
}


void CEGUIScrollBar::SetPictureBox(PicIndex index, CEGUIPictureBox *pic)
{
	switch(index) {
	case BAR_BEGIN:
		SAFE_DELETE(m_pPicBarBegin);
		m_pPicBarBegin = pic;
		break;
	case BAR_END:
		SAFE_DELETE(m_pPicBarEnd);
		m_pPicBarEnd = pic;
		break;
	case BAR_MIDDLE:
		SAFE_DELETE(m_pPicBarMiddle);
		m_pPicBarMiddle = pic;
		break;
	case BAR_TARGET:
		SAFE_DELETE(m_pPicTarget);
		m_pPicTarget = pic;
		break;
	}
}


void CEGUIScrollBar::SetValue(float value)
{
	Math::Clamp<float>(value, 0, 1);
	m_fValue = value;
	_UpdateTarget();
}